
import axios from './axios'
import obj from './api'
import { getcookie } from '@/utils/cookie'
var {types,banner,cart,user,video,address,waitpay,waitsend,waitreceive,waitsales,likevideo}=obj

export function getbanner()
{
    return axios.get(banner)
}
export function getTypes()
{
    return axios.get(types).then(data=>{
        var types = data.map(type=>{
            delete type.products
            return type
        })
        return new Promise(function(resolve,reject)
        {
            resolve(types)
        })

    })
}
export function getTP()
{
    return axios.get(types);
}
export function getpro(typename)
{
    return axios.get(types).then(data=>{
        if(typename=='全部')
        {

        var arr = []
        data.forEach(type => {
            arr = arr.concat(type.products)
        });
        // return new Promise(resolve => resolve(arr))
    } else{
        var arr = data.find(type => type.name === typename).products
    }
    return new Promise(resolve => resolve(arr))
    })
}
export function getvideo()
{
    return axios.get(video)
}
export function regist(params)
{
    return axios.get(user).then(data=>{
        var index = data.findIndex(item=>item.username===params.username)
        if(index<0)
        {
            return axios.post(user,params).then(data=>{
                return new Promise(resolve=>resolve({
                    code:0,
                    msg:"注册成功"
                }))

            })
        }else
        {
            return new Promise(resolve=>resolve({
                code:1,
                msg:"账号被占用"
            }))
        }
    })
}
export function login(params,input,check) {
    return axios.get(user).then(users => {
    var index = users.findIndex(item => item.username === params.username &&
    item.password === params.password)

        if(index < 0) {
            var responseObj = {
            code: 0,
            msg: '账号或密码错误'
            
            }
            } else {
                if(input===check)
                {
                    var responseObj = {
                        code: 1,
                        msg: '登录成功',
                        data:{
                            username:users[index].username,
                            nickname:users[index].nickname,
                        }
                        }
                        }else{
                            var responseObj = {
                                code: 0,
                                msg: '验证码错误，请重新输入'
                                
                                }
                        }
                }
          
    return new Promise(resolve => resolve(responseObj))
    })
    }
export function addCart(product)
{
    return axios.get(cart).then(carts=>{
        var cartpro = carts.find(item=>item.pid === product.pid && item.username === product.username)
        if(cartpro)
        {
            cartpro.number++;
            return axios.put(cart + '/' + cartpro.id,cartpro).then(data=>{
                return new Promise(resolve=>resolve({
                    code:1,
                    msg:'添加成功'
                }))

            })
        }else{
            return axios.post(cart,product).then(data=>{
                  return new Promise(resolve=>resolve({
                    code:1,
                    msg:'添加成功'
                  }))  
            })
        }

    })
}
export function getcart()
{
    return axios.get(cart).then(data=>{
        var username = getcookie('username')
        data = data.filter(item=>item.username === username)
        return new Promise(resolve=>resolve(data))
    });
}
export function editCart(params)
{
    return axios.put(cart + '/' + params.id,params).then(data=>{
        return new Promise(resolve=>resolve({
            code:1,
            msg:'数量修改成功'
        }))
    })
}
export function operate(num1,num2,operator)
{
    //找到两个数字哪个小数比较多
    //将数字先转化为字符串
    var newNum1 = num1 + ''
    var newNum2 = num2 + ''
    //找数字中小数点的下标
    var num1DotIndex = newNum1.indexOf('.')
    var num2DotIndex = newNum2.indexOf('.')
    //判断是否都为整数
    if(num1DotIndex===-1&&num2DotIndex===-1)
    {
        return eval(num1 + operator + num2) 
    }
    //判断第一个数为整数
    if(num1DotIndex===-1)
    {
        var num2Suffix = newNum2.slice(num2DotIndex+1)
        var max = num2Suffix.length
    }else if(num2DotIndex===-1){
        //判断第二个数为整数
        var num1suffix = newNum1.slice(num1DotIndex+1)
        var max = num1suffix.length
    }else{
        //两个数都不是整数
        var num1suffix = newNum1.slice(num1DotIndex+1)
        var num2Suffix = newNum2.slice(num2DotIndex+1)
        //判断哪个大
        var max = num1suffix.length
        if(num2Suffix.length>num1suffix.length)
        {
            max = num2Suffix.length
        }
    }
    //乘10的max次方，让这两个数都变成整数
    var num1zheng = num1*Math.pow(10,max)
    var num2zheng = num2*Math.pow(10,max)
    switch(operator)
    {
        case '+':
            var result = num1zheng+num2zheng
            break
        case '-':
            var result = num1zheng-num2zheng
            break
        case '*':
            var result = num1zheng*num2zheng
            result /= Math.pow(10,max)
            break
        case '/':
            var result = num1zheng/num2zheng
            result+=result.toFixed(2)
            break
        default:
            throw new Error('请输入正确的运算符')
    }
    //再将结果除以10的max次方
    return result / Math.pow(10,max)
}
export function delCart(id)
{
    return axios.delete(cart + '/' + id).then(data=>{
        return new Promise(resolve=>resolve({
            code:1,
            msg:'购物车商品删除成功！'
        }))
    })
    
}
export function getAddr()
{
    return axios.get(address).then(data=>{
        var username = getcookie('username')
        data = data.filter(item=>item.username === username)
        return new Promise(resolve=>resolve({
            code:1,
            msg:'地址获取成功',
            data:data
        }))
    })
}
export function saveAddr(params)
{
    if(params.isDefault)
    {
        return axios.get(address).then(data=>{
            var username = getcookie('username')
            // 如何判断是添加还是修改
            // 添加的时候，数据中没有id，修改的时候数据中有id - 拿id判断是修改还是添加
            if(params.id)
            {
                if(params.isDefault)
                {
                    //要修改的是默认地址
                    //先将原本的默认改为非默认
                    //获取所有地址
                    return axios.get(address).then(data=>{
                        data = data.filter(item=>item.username === username)
                        //找到当前地址
                        var defaultAddr = data.find(item => item.isDefault)
                        //判断是否找到了
                        if(defaultAddr){
                            //找到了就改为非默认
                            defaultAddr.isDefault = false
                            return axios.put(address + '/' + defaultAddr.id,defaultAddr).then(data=>{
                                return new Promise(resolve=>resolve({
                                    code:1,
                                    msg:'地址修改成功',
                                }))
                            })
                        }else{
                            //没找到就不管了，直接改
                            return axios.put(address + '/' + params.id,params).then(data=>{
                                return new Promise(resolve=>resolve({
                                    code:1,
                                    msg:'地址修改成功',
                                }))
                            })
                        }
                    })
                }else{
                    //修改的不是默认地址
                    //直接修改
                    return axios.put(address + '/' + params.id + params).then(data=>{
                        return new Promise(resolve=>resolve({
                            code:1,
                            msg:'地址修改成功'
                        }))
                    })
                }
            }else{
                //添加
                //判断要添加的是不是默认地址
                if(params.isDefault) {
                    // 添加的是默认地址
                    // 先将原本的默认地址改成非默认地址
                    // 获取当前用户的默认地址
                    return axios.get(address).then(data => {
                        // 过滤出当前用户的所有地址
                        
                        data = data.filter(item => item.username === username)
                        // 从当前用户的所有地址中找出默认地址
                        var defaultAddr = data.find(item => item.isDefault)
                        // 判断是否找到了默认地址
                        if(defaultAddr) {
                            // 找到了默认地址
                            // 将这个默认地址先改成非默认地址
                            defaultAddr.isDefault = false
                            return axios.put(address + '/' + defaultAddr.id, defaultAddr).then(data => {
                                // 改完后，再添加新的默认地址
                                return axios.post(address, params).then(data => {
                                    return new Promise(resolve => resolve({
                                        code: 1,
                                        msg: '地址保存成功'
                                    }))
                                })
                            })
                        } else {
                            // 没有找到默认地址
                            return axios.post(address, params).then(data => {
                                return new Promise(resolve => resolve({
                                    code: 1,
                                    msg: '地址保存成功'
                                }))
                            })
                        }
                    })
                } else {
                    // 添加的是非默认地址
                    return axios.post(address, params).then(data => {
                        return new Promise(resolve => resolve({
                            code: 1,
                            msg: '地址保存成功'
                        }))
                    })
                }
            }
            
    }
)}
}
export function delAddr(id)
{
    return axios.delete(address + '/' + id).then(data=>{
        return new Promise(resolve=>resolve({
            code:1,
            msg:'地址删除成功'
        }))
    })
}
// 获取默认地址
export function getDefaultAddr() {
    // 获取所有地址
    return axios.get(address).then(data => {
    // 获取到当前用户的地址
        var username = getcookie('username')
        data = data.filter(item => item.username === username)
        // 判断当前用户是否有地址
        if(data.length) {
        // 从中找到默认地址
            var defaultAddr = data.find(item => item.isDefault)
        // 判断是否找到
            if(defaultAddr) {
                return new Promise(resolve => resolve({
                code: 1,
                msg: '默认地址获取成功',
                data: defaultAddr
                }))
             } else {
    // 没有默认地址
             return new Promise(resolve => resolve({
                code: 1,
                msg: '默认地址获取成功',
                data: data[0]
    }))
        }
                } else {
                    return new Promise(resolve => resolve({
                        code: 2,
                        msg: '用户暂未添加地址',
                        data: null
        }))
    }
    })
    }
export function delall(params)
{
    var id = params.map(item=>item.id)
    //promise all方法，所有异步请求完成才返回
    var arr = []
    id.forEach(item=>{
        arr.push(new Promise(resolve=>{
            axios.delete(cart + '/' + item).then(data=>{
                resolve({
                    code:1,
                    msg:'删除一个'
                })
            })
        }))
    })
    return Promise.all(arr).then(data=>{
        if(data.length === arr.length)
        {
            return new Promise(resolve => resolve({
                code:111,
                msg:'全部删除成功'
            }))
        }else{
            return new Promise(resolve=>resolve({
                code:222,
                msg:删除失败
            }))
        }
    })
}
// 获取零钱
export function getmoney()
{
        return axios.get(user).then(data=>{
        var username = getcookie('username')
        data = data.find(item=>item.username === username)
        return new Promise(resolve=>resolve(data))
    })
}
//放入待发货
export function towaitsend(params)
{
    params.forEach(item=>{
        return axios.post(waitsend,item).then(data=>{
            return new Promise(resolve=>resolve({
                code:1,
                msg:'放入待付款成功'
            }))
        })
            
    })
        
   
}
//放入待付款
export function towaitpay(params)
{
    params.forEach(item=>{
        return axios.post(waitpay,item).then(data=>{
            return new Promise(resolve=>resolve({
                code:1,
                msg:'放入待付款成功'
            }))
        })
    })
    
}
//修改用户昵称
export function rename(value){
    return axios.get(user).then(data => {
        var username = getcookie('username')
        data=data.find(item => item.username===username)
        data.nickname = value
        return axios.put(user+'/'+data.id,data).then(item => {
            return new Promise(resolve => resolve({
                code:1,
                msg:'修改成功',
                item:data
            }))
        })
    })
}
//注销用户
export function deluser(){
    return axios.get(user).then(data => {
        var username = getcookie('username')
        data=data.find(item => item.username===username)
        return axios.delete(user+'/'+data.id).then(item => {
            return new Promise(resolve => resolve({
                code:1,
                msg:'注销成功',
                item:data
            }))
        })
    })
}
//充值零钱
export function recharge(value){
    return axios.get(user).then(data => {
        var username = getcookie('username')
        data=data.find(item => item.username===username)
        var money=parseFloat(value)
        if(data.money){
            data.money+=money
        }else{
            data.money=money
        }
        return axios.put(user+'/'+data.id,data).then(item => {
            return new Promise(resolve => resolve({
                code:1,
                msg:'充值成功',
                item:data
            }))
        })
        
    })
}
//找到用户的零钱
export function findmoney(){
    return axios.get(user).then(data=>{
        var username = getcookie('username')
        data=data.find(item => item.username===username)
        return new Promise(resolve=> resolve(data))
    })
}
//更新零钱
export function setmoney(money)
{
    money = parseFloat(money)
    return axios.get(user).then(data=>{
        var username = getcookie("username")
        data = data.find(item=>item.username === username)
        data.money = money
        return axios.put(user + '/' + data.id,data).then(item=>{
            return  new Promise(resolve=>resolve({
                code:1,
                msg:'零钱修改成功',
                item:data
            }))
        })
    })
}
//查询待付款
export function my_orders(){
    return axios.get(waitpay).then(data=>{
        var username = getcookie('username')
        data=data.filter(item => item.username===username)
        return new Promise(resolve=> resolve(data))
    })
}
//待付款付款后删除付款的商品增加到待收货
export function change_my_orders(id){
    return axios.get(waitpay).then(data=>{
        var username = getcookie('username')
        data=data.filter(item => item.username===username)
        data.forEach(item=>{
            if(item.id===id){
                return axios.post(waitsend,item)
            }
        })
        data.forEach(item=>{
            if(item.id===id){
                return axios.delete(waitpay+'/'+item.id)
            }
        })
    })
}

//查询待发货
export function find_goods_orders(){
    return axios.get(waitsend).then(data=>{
        var username = getcookie('username')
        data=data.filter(item => item.username===username)
        return new Promise(resolve=> resolve(data))
    })
}
//待发货过10秒后自动跳到待收货
export function change_receiving(){
    //延时函数
    setTimeout(() => {
        return axios.get(waitsend).then(data=>{
            var username = getcookie('username')
            data=data.filter(item => item.username===username)
            //
            data.forEach(item => {
                return axios.post(waitreceive,item)
            })
            data.forEach(item => {
                return axios.delete(waitsend+'/'+item.id)
            })
            return new Promise(resolve=> resolve(data))
        })
    }, 10000);
}
//查询待收货
export function find_receiving_orders(){
    return axios.get(waitreceive).then(data=>{
        var username = getcookie('username')
        data=data.filter(item => item.username===username)
        return new Promise(resolve=> resolve(data))
    })
}
//确认收货
export function confirm_goods(){
    return axios.get(waitreceive).then(data=>{
        var username = getcookie('username')
        data=data.filter(item => item.username===username)
        //
        data.forEach(item => {
            console.log("item.id",item.id)
            return axios.post(waitsales,item)
        })
        data.forEach(item => {
            return axios.delete(waitreceive+'/'+item.id,item)
        })
        return new Promise(resolve=> resolve(data))
    })
}
//查询待售后
export function find_sales_orders(){
    return axios.get(waitsales).then(data=>{
        var username = getcookie('username')
        data=data.filter(item => item.username===username)
        return new Promise(resolve=> resolve(data))
    })
}
//修改密码
export function repassword(params) {
    return axios.get('user').then(users => {
        var username=getcookie('username')
         //var a=users.find(item=>item.username===params.username)
        if(params.username===username){
                var a=users.find(item=>item.username===params.username)
                var index = users.findIndex(item => item.username === params.username &&
                item.password === params.password)
                            if(index < 0) {
                                    return new Promise(resolve => resolve({
                                            code: 0,
                                            msg: '账号或密码错误'
                                            })
                                        )
                            } else {
                                    if(a.password!=params.repass){
                                            a.password=params.repass
                                            console.log(a)
                                            return axios.put('user'+ '/'+ a.id,a).then(data => { 
                                            return new Promise(resolve => resolve({
                                                    code: 1,
                                                    msg: '修改成功'
                                            })) 
                                    })
                                    }else{
                                            console.log("新密码和旧密码一样")
                                            return new Promise(resolve => resolve({
                                                    code: 2,
                                                    msg: '新密码和旧密码一样'
                                                    })
                                                )
             }
            }
            }else{
                    //console.log("与当前用户名不匹配")
                    return new Promise(resolve => resolve({
                            code: 0,
                            msg: '账号或密码错误'
                            })
                        )
            }
            
    })
}
//获取喜欢的视频
export function getLikeVideo(){
    return axios.get(likevideo).then(data =>{
        var username = getcookie('username')
        data = data.filter(item => item.username === username)
        return new Promise(resolve => resolve(data))
    })
}
//添加到我喜欢的视频
export function addVideo(lvideo)
    {
        return axios.get(likevideo).then(data=>{
            var username = getcookie('username')
            data = data.filter(videos=>videos.username===username)
            var flag = data.find(item=>item.pid === lvideo.pid)
            if(flag)
            {
                return new Promise(resolve=>resolve({
                    code:0,
                    msg:'已加入喜欢，无需添加'
                }))
            } else{
                return axios.post(likevideo,lvideo).then(data=>{
                return new Promise(resolve=>resolve({
                    code:1,
                    msg:'已添加到喜欢'
                }))
            })
            }
        })
    }